package com.youyou.fusheng2;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.youyou.fusheng2.Moudle.SouSuoImageCliCk;
import com.youyou.fusheng2.View.Fragment.FenleiFragment;
import com.youyou.fusheng2.View.Fragment.HomeFragment;
import com.youyou.fusheng2.View.Fragment.MineFragment;
import com.youyou.fusheng2.View.Fragment.SousuoFragment;

import java.util.List;

public class FuShengMain extends AppCompatActivity implements  SouSuoImageCliCk, BottomNavigationView.OnNavigationItemSelectedListener {

    private HomeFragment homeFragment = new HomeFragment();
    private FenleiFragment fenleiFragment = new FenleiFragment();
    private SousuoFragment sousuoFragment = new SousuoFragment();
    private MineFragment mineFragment = new MineFragment();
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private FrameLayout mMainContext;
    private BottomNavigationView mMainBnv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fushengmain);
        initView();
        initFragment();
    }

    private void initFragment() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  17:09
         *  @简介: 初始化fragment的管理器
         */
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.main_context, homeFragment).add(R.id.main_context, fenleiFragment).add(R.id.main_context, sousuoFragment).add(R.id.main_context, mineFragment)
                .hide(fenleiFragment).hide(sousuoFragment).hide(mineFragment)
                .show(homeFragment)
                .commit();
    }

    private void initView() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  17:05
         *  @简介: 初始化布局
         */
        FrameLayout main_context = (FrameLayout) findViewById(R.id.main_context);
        RadioButton main_home = (RadioButton) findViewById(R.id.main_home);
        RadioButton main_fenlei = (RadioButton) findViewById(R.id.mian_fenlei);
        RadioButton main_sousuo = (RadioButton) findViewById(R.id.main_sousuo);
        RadioButton main_mine = (RadioButton) findViewById(R.id.main_mine);
        mMainBnv = (BottomNavigationView) findViewById(R.id.main_bnv);
        mMainBnv.setOnNavigationItemSelectedListener(this);
        fenleiFragment.setOnClickListner(this);
        homeFragment.setOnClickListner(this);
    }



    private void showFragment(Fragment fragment) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  17:24
         *  @简介: 判断显示那个fragment
         *  */
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        //遍历fragment的集合 其他的fragment  显示当前的fragment
        List<Fragment> fragmentList = fragmentManager.getFragments();
        for (Fragment f : fragmentList) {
            transaction.hide(f);
        }
        transaction.show(fragment).commit();
    }

    @Override
    public void OnImageCliCk(View view) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/27  01:30
         *  @简介: 实现自定义接口 通过
         *         fenleiFragment.setOnClickListner(this);
         *         homeFragment.setOnClickListner(this);
         *         进行跳转到相应页面
         */
        switch (view.getId()) {
            case R.id.home_sousuo:
                showFragment(sousuoFragment);
                mMainBnv.setSelectedItemId(mMainBnv.getMenu().getItem(2).getItemId());
                break;
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.item_home:
                showFragment(homeFragment);
                break;
            case R.id.item_fenlei:
                showFragment(fenleiFragment);
                break;
            case R.id.item_sousuo:
                showFragment(sousuoFragment);
                break;
            case R.id.item_mine:
                showFragment(mineFragment);
                break;
        }
        return true;
    }

}